//
//  CustomSlider.m
//  DGCircleDemo
//
//  Created by HunDegong on 2024/12/6.
//

#import "HDGCustomSlider.h"
#import "HDGDragView.h"

@interface HDGCustomSlider ()

@property (nonatomic, weak, readwrite) HDGDragView *leftDrag;
@property (nonatomic, weak, readwrite) HDGDragView *rightDrag;
@property (nonatomic, weak, readwrite) UIView *backgroundView;
@property (nonatomic, weak, readwrite) UIView *foregroundView;

@property (nonatomic, copy) void(^sliderHandler)(float minValue, float maxValue);
@property (nonatomic, copy) void(^leftSliderHandler)(float minValue);
@property (nonatomic, copy) void(^rightSliderHandler)(float maxValue);

@end

@implementation HDGCustomSlider

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialization];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self initialization];
    }
    return self;
}

- (void)initialization
{
    [self customUI];
    
    [self prepareData];
}

- (void)customUI
{
    // Background View
    UIView *backgroundView = [[UIView alloc] init];
    backgroundView.backgroundColor = [UIColor whiteColor];
    [self addSubview:backgroundView];
    self.backgroundView = backgroundView;
    
    // Foreground View
    UIView *foregroundView = [[UIView alloc] init];
    foregroundView.backgroundColor = [UIColor colorWithRed:101.0/255.0 green:229.0/255.0 blue:216.0/255.0 alpha:0.5]; // Light blue
    [self addSubview:foregroundView];
    self.foregroundView = foregroundView;
    
    // left drag
    HDGDragView *leftDrag = [[HDGDragView alloc] init];
    leftDrag.userInteractionEnabled = YES;
    [self addSubview:leftDrag];
    self.leftDrag = leftDrag;
    
    UIPanGestureRecognizer *leftPanGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(leftDragHandlePanGesture:)];
    [leftDrag addGestureRecognizer:leftPanGesture];
    
    // right drag
    HDGDragView *rightDrag = [[HDGDragView alloc] init];
    rightDrag.userInteractionEnabled = YES;
    [self addSubview:rightDrag];
    self.rightDrag = rightDrag;
    
    UIPanGestureRecognizer *rightPanGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(rightDragHandlePanGesture:)];
    [rightDrag addGestureRecognizer:rightPanGesture];
}

- (void)prepareData
{
    self.minValue = 0;
    self.maxValue = 100;
    self.dragMinSpacing = 30;
    self.leftDragMaxValue = 60;
    self.rightDragMinValue = 40;
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    CGFloat leftDragW = [HDGDragView widthWidthText:[NSString stringWithFormat:@"%d", (int)self.minValue]];
    CGFloat rightDragW = [HDGDragView widthWidthText:[NSString stringWithFormat:@"%d", (int)self.maxValue]];
    CGFloat dragH = [HDGDragView fetchHeight];
    
    CGFloat width = self.bounds.size.width;
    CGFloat bgLeft = leftDragW * 0.5;
    CGFloat fgRight = rightDragW * 0.5;
    CGFloat bgW = width - bgLeft - fgRight;
    
    CGFloat lineHeight = 2.0;
    CGFloat dragTopImgWH = [HDGDragView defaultPointIconWidth];
    CGFloat bgY = (dragTopImgWH - lineHeight) * 0.5;
    self.backgroundView.frame = CGRectMake(bgLeft, bgY, bgW, lineHeight);
    
    CGFloat fgX = self.minValue / 100.0 * bgW + bgLeft;
    CGFloat fgW = (self.maxValue - self.minValue) / 100.0 * bgW;
    self.foregroundView.frame = CGRectMake(fgX, bgY, fgW, lineHeight);
    
    CGFloat leftDragX = fgX - leftDragW * 0.5;
    self.leftDrag.frame = CGRectMake(leftDragX, 0, leftDragW, dragH);
    
    CGFloat rightDragX = self.maxValue / 100.0 * bgW + bgLeft - rightDragW * 0.5;
    self.rightDrag.frame = CGRectMake(rightDragX, 0, rightDragW, dragH);
}

- (void)leftDragHandlePanGesture:(UIPanGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan) {
        // 手势开始时，可以做一些初始化操作，比如记录初始位置等
    } else if (gesture.state == UIGestureRecognizerStateChanged) {
        // 获取手势的位移
        CGPoint translation = [gesture translationInView:self.leftDrag];
        // 获取被拖动的视图
        UIView *viewBeingDragged = gesture.view;
        // 更新视图的位置，这里简单地在原来位置基础上加上位移量
        CGFloat x = viewBeingDragged.center.x + translation.x;
        CGFloat minX = self.leftDrag.bounds.size.width * 0.5;
        if (x < minX) x = minX;
        CGFloat maxX = CGRectGetMidX(self.rightDrag.frame) - self.backgroundView.frame.size.width * self.dragMinSpacing / 100.0;
        if (x > maxX) x = maxX;
        CGFloat leftDragMaxX = minX + self.backgroundView.frame.size.width * self.leftDragMaxValue / 100.0;
        if (x > leftDragMaxX) x = leftDragMaxX;
        viewBeingDragged.center = CGPointMake(x, viewBeingDragged.center.y);
        // 重置位移量，避免位移累计导致位置异常
        [gesture setTranslation:CGPointZero inView:self.leftDrag];
        // 更新前景色
        [self updateForcegroundViewWithLeftDrag:YES centerX:x];
    } else if (gesture.state == UIGestureRecognizerStateEnded) {
        // 手势结束时，可以做一些收尾操作，比如根据最终位置进行对齐等
    }
}

- (void)rightDragHandlePanGesture:(UIPanGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan) {
        // 手势开始时，可以做一些初始化操作，比如记录初始位置等
    } else if (gesture.state == UIGestureRecognizerStateChanged) {
        // 获取手势的位移
        CGPoint translation = [gesture translationInView:self.rightDrag];
        // 获取被拖动的视图
        UIView *viewBeingDragged = gesture.view;
        // 更新视图的位置，这里简单地在原来位置基础上加上位移量
        CGFloat x = viewBeingDragged.center.x + translation.x;
        CGFloat maxX = CGRectGetMaxX(self.backgroundView.frame);
        if (x > maxX) x = maxX;
        CGFloat minX = CGRectGetMidX(self.leftDrag.frame) + self.backgroundView.frame.size.width * self.dragMinSpacing / 100.0;
        if (x < minX) x = minX;
        CGFloat rightDragMinX = self.leftDrag.bounds.size.width * 0.5 + self.backgroundView.frame.size.width * self.rightDragMinValue / 100.0;
        if (x < rightDragMinX) x = rightDragMinX;
        viewBeingDragged.center = CGPointMake(x, viewBeingDragged.center.y);
        // 重置位移量，避免位移累计导致位置异常
        [gesture setTranslation:CGPointZero inView:self.rightDrag];
        // 更新前景色
        [self updateForcegroundViewWithLeftDrag:NO centerX:x];
    } else if (gesture.state == UIGestureRecognizerStateEnded) {
        // 手势结束时，可以做一些收尾操作，比如根据最终位置进行对齐等
    }
}

- (void)setMinValue:(float)minValue
{
    _minValue = minValue;
    self.leftDrag.titleLabel.text = [NSString stringWithFormat:@"%d", (int)minValue];
    [self setupForcegroundFrame];
}

- (void)setMaxValue:(float)maxValue
{
    _maxValue = maxValue;
    self.rightDrag.titleLabel.text = [NSString stringWithFormat:@"%d", (int)maxValue];
    [self setupForcegroundFrame];
}

- (void)setupForcegroundFrame
{
    CGFloat lineHeight = 2.0;
    CGFloat dragTopImgWH = [HDGDragView defaultPointIconWidth];
    CGFloat bgY = (dragTopImgWH - lineHeight) * 0.5;
    CGFloat fgX = self.minValue / 100.0 * self.backgroundView.bounds.size.width + self.backgroundView.frame.origin.x;
    CGFloat fgW = (self.maxValue - self.minValue) / 100.0 * self.backgroundView.bounds.size.width;
    self.foregroundView.frame = CGRectMake(fgX, bgY, fgW, lineHeight);
}

- (void)updateForcegroundViewWithLeftDrag:(BOOL)isLeft centerX:(CGFloat)centerX
{
    if (isLeft) {
        float minValue = (centerX - self.backgroundView.frame.origin.x) / self.backgroundView.frame.size.width * 100;
        self.minValue = minValue;
        if (self.leftSliderHandler) {
            self.leftSliderHandler(minValue);
        }
    } else {
        float maxValue = (centerX - self.backgroundView.frame.origin.x) / self.backgroundView.frame.size.width * 100;
        self.maxValue = maxValue;
        if (self.rightSliderHandler) {
            self.rightSliderHandler(maxValue);
        }
    }
    if (self.sliderHandler) {
        self.sliderHandler(self.minValue, self.maxValue);
    }
}

- (void)dragSliderHandler:(void(^)(float minValue, float maxValue))sliderHandler
{
    self.sliderHandler = sliderHandler;
}

- (void)dragLeftSliderHandler:(void (^)(float))leftSliderHandler
{
    self.leftSliderHandler = leftSliderHandler;
}

- (void)dragRightSliderHandler:(void (^)(float))rightSliderHandler
{
    self.rightSliderHandler = rightSliderHandler;
}

+ (CGFloat)defaultHeight
{
    return [HDGDragView fetchHeight];
}

@end
